package com.elh.jvm;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import lombok.Data;

import java.util.List;

/**
 * 主要用于接收命令行参数
 *
 * @author liulei
 **/
@Data
public class Args {

    @Parameter(names = {"?", "-helper"},description = "print help message", order = 3, help = true)
    boolean helpFlag = false;

    @Parameter(names = "-version", description = "print version and exit", order = 2)
    boolean versionFlag = false;

    @Parameter(names = { "-cp", "-classpath" }, description = "classpath", order = 1)
    String classpath;

    /**
     * 所有其他没有解析的options
     *
     */
    @Parameter(description = "main class and args")
            List<String> mainClassAndArgs;

    boolean ok;


    public static Args parse(String[] argv) {
        // https://www.cnblogs.com/brainstorm/p/7562407.html
        Args args = new Args();
        JCommander cmd = JCommander.newBuilder()
                .addObject(args)
                .build();
        try {
            cmd.parse(argv);
            args.ok = true;
        } catch (ParameterException ignored) {

        }

        return args;
    }

}
